﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using BLL;

public partial class EditFinaceRecord : System.Web.UI.Page
{
    private FinaceRecordBll bll = new FinaceRecordBll();

    public bool IsUpdate
    {
        get
        {
            return Convert.ToBoolean(Request.QueryString["IsUpdate"]);
        }
    }

    public int FinaceTypeID
    {
        get
        {
            if (!this.IsUpdate && Request.QueryString["FinaceTypeID"] == null)
            {
                throw new ApplicationException("FinaceTypeID is need");
            }

            return int.Parse(Request.QueryString["FinaceTypeID"]);
        }
    }

    public int FinaceID
    {
        get
        {
            if (this.IsUpdate && Request.QueryString["FinaceID"] == null)
            {
                throw new ApplicationException("FinaceID is need");
            }

            return int.Parse(Request.QueryString["FinaceID"]);
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.InitUI();
        }
    }

    private void InitUI()
    {
        if (this.IsUpdate)
        {
            this.InitUpdateUI();
        }
        else
        {
            this.InitAddUI();
        }
    }

    private void InitUpdateUI()
    {
        FinaceRecord record = bll.SelectSingleFinaceRecord(this.FinaceID);

        this.lblPayTypeName.Text = record.FinaceType.TypeName;
        this.txtAmount.Text = record.Amount.ToString();
        this.txtDetail.Text = record.Detail.ToString();
        this.txtPayTime.Text = record.PayTime.ToString();
    }

    private void InitAddUI()
    {
        FinaceTypeBll payTypeBll = new FinaceTypeBll();
        FinaceType finaceType = payTypeBll.SelectSingleFinaceType(this.FinaceTypeID);

        this.lblPayTypeName.Text = finaceType.TypeName;
        txtPayTime.Text = DateTime.Now.ToString();
    }

    private void UIToModel(FinaceRecord model)
    {
        model.Amount = decimal.Parse(this.txtAmount.Text);
        model.Detail = this.txtDetail.Text;
        model.PayTime = DateTime.Parse(this.txtPayTime.Text);
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        FinaceRecord model = null;

        if (!this.IsUpdate)
        {
            model = new FinaceRecord();
            model.FinaceTypeID = this.FinaceTypeID;
            model.MemberID = HttpMemberInfo.MemerID;
            this.UIToModel(model);

            bll.AddFinaceRecord(model);

        }
        else
        {
            model = bll.SelectSingleFinaceRecord(this.FinaceID);
            this.UIToModel(model);

            bll.UpdateFinaceRecord(model);
        }


        //RefreshData
        string script = "Close();window.parent.RefreshData();";

        this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "@script", script, true);
    }

}
